# -*- coding: UTF-8 -*-

from game2048 import Game2048, UP, DOWN, LEFT, RIGHT
from RL_brain import QLearningTable  #class
import time
import random
key_map = {'Up': UP, 'Down': DOWN, 'Left': LEFT, 'Right': RIGHT}


def input_listener(event=None):
    key = '{}'.format(event.keysym)
    if key in key_map:
        game.step(key_map[key])

def update():
    max = 0
    # 定义学习的回合数
    for episode in range(10000):
        # 初始化 state 的观测值
        observation = game.reset()
        # game.reset()
        while True:
            # 更新界面
            game.update()
            # 界面延迟观察
            # RL 大脑根据 state 的观测值挑选 action
            action = RL.choose_action(str(observation))

            # 探索者在环境中实施这个 action, 并得到环境返回的下一个 state 观测值,
            observation_, reward, done = game.step(action)

            # RL 从这个序列 (state, action, reward, state_) 中学习
            RL.learn(str(observation), action, reward, str(observation_))

            # 将下一个 state 的值传到下一次循环  更新到下一个状态中去
            observation = observation_

            # 如果回合结束，进入下一轮
            if done:
                max = game.get_score() if max<game.get_score() else max
                print ('episode:' , episode , 'score:' , game.get_score() , 'row:' , len(RL.q_table) ,'max:' , max)
                break
    print (RL.q_table)
    print('game over')


if __name__ == '__main__':
    game = Game2048()
    RL = QLearningTable(actions=list(range(game.n_actions)))
    game.bind_all('<Key>', lambda event: input_listener(event))
    game.after(100, update())
    game.mainloop()

